package com.china08.yxyapi.api.manage.tec.office.website.article;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSection;
import com.china08.yxyapi.entity.mg.sws.SwsArticle;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSectionRepository;
import com.china08.yxyapi.repository.mg.SwsArticleRepository;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.util.HtmlCompressorUtil;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class SwsTeaArticleUpdController extends YxyApiBaseController {

    @Autowired
    SwsSectionRepository swsSectionRepository;

    @Autowired
    SwsArticleRepository swsArticleRepository;

    @ApiOperation("教师修改资讯")
    @RequestMapping(value = { "/api_yw/manage/teaching/article/upd",
            "/api_yw/manage/tec/office/website/article/upd" }, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public YxyApiRespModel precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) {
        YxyProfile yxyProfile = getProfile(request, response);
        String senderId = yxyProfile.getId();
        String id = reqModel.id;
        SwsArticle swsArticle = swsArticleRepository.findById(id);
        Assert.isTrue(!(swsArticle.getAuditStatus() == 1), "审核通过的不能修改");
        Assert.notNull(swsArticle, "资讯不存在");
        Assert.isTrue(StringUtils.equals(senderId, swsArticle.getSenderId()), "资讯不存在");
        Assert.isTrue(swsArticle.getAuditStatus().intValue() != 1, "审核已通过禁止修改");
        String section1 = reqModel.section1;
        String section2 = reqModel.section2;
        String title = reqModel.title;
        String author = reqModel.auther;
        String content = reqModel.content;
        content = HtmlCompressorUtil.HtmlCompressor(content);
        Assert.isTrue(StringUtils.length(Jsoup.parse(content).text()) <= 20000, "内容禁止超过2万字");
        String senderName = yxyProfile.getUserNick();
        String ownerSchoolId = yxyProfile.getSchoolId();
        String attachmentFileName = reqModel.attachmentFileName;
        String attachmentDownloadUrl = reqModel.attachmentDownloadUrl;

        // 设置显示时间
        // 获取详情中的第一张图片作为推荐图片
        String faceImg = "";
        Document document = Jsoup.parse(content);
        Elements elements = document.select("img");
        if (elements.size() > 0) {
            faceImg = elements.get(0).attr("src");
        }

        // 判断是否有二级栏目
        List<SwsSection> swsSection2List = swsSectionRepository.findBySchoolIdAndPidOrderByOrderNumAsc(ownerSchoolId,
                section1);
        if (StringUtils.isBlank(section2) && swsSection2List.size() > 0) {
            Assert.isTrue(false, "请选择二级栏目");
        }

        // 审核状态
        Integer auditStatus = 0;

        // 判断当前栏目是否需要审核，设置文章AuditStatus状态
        // 发送到二级
        if (StringUtils.isNotBlank(section2)) {
            SwsSection section = swsSectionRepository.findById(section2);
            Assert.isTrue(null != section, "二级栏目为空");
            auditStatus = section.getIsAudit() ? 0 : 1;
        }
        // 发送到一级
        else {
            SwsSection section = swsSectionRepository.findById(section1);
            Assert.isTrue(null != section, "一级栏目为空");
            auditStatus = section.getIsAudit() ? 0 : 1;
        }
        swsArticle.setTitle(title);
        swsArticle.setAuthor(author);
        swsArticle.setSection1Id(section1);
        swsArticle.setSection2Id(section2);
        swsArticle.setFaceImg(faceImg);
        swsArticle.setSummary("");
        swsArticle.setContent(content);
        swsArticle.setSenderName(senderName);
        swsArticle.setAuditStatus(auditStatus);
        swsArticle.setAttachmentFileName(attachmentFileName);
        swsArticle.setAttachmentDownloadUrl(attachmentDownloadUrl);
        swsArticleRepository.save(swsArticle);
        return new YxyApiRespModel("修改成功");
    }

    public static class ReqModel extends YxyApiPageReqModel {
        public String id;
        public String section1;
        public String section2;
        public String title;
        public String auther;
        public String content;
        public String attachmentFileName;
        public String attachmentDownloadUrl;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(title), "标题不能为空");
            Assert.isTrue(title.length() > 1 && title.length() < 37, "请输入2-36个字的标题");
            Assert.isTrue(StringUtils.isNotBlank(section1), "一级栏目不能为空");
            Assert.isTrue(StringUtils.isNotBlank(content), "内容不能为空");
            if (StringUtils.isNotBlank(auther)) {
                Assert.isTrue(auther.length() < 7 && auther.length() > 1, "作者要在2-6个字");
            }
        }
    }

}
